package com.easy.thread;

import java.util.concurrent.TimeUnit;

/**
 * @Packge com.easy.thread
 * @Autor linNaibin
 * @Description
 * @Version 1.0
 * @Date 2021/9/28 12:21
 */
public class ThreadTest {

    private static volatile int state = 0;

    public static void main(String[] args) {

        new Thread(() -> {

            while (state == 0) {
                state = 1;
                System.out.println("[线程：" + Thread.currentThread().getName() + "]加锁成功!");
                try {
                    TimeUnit.SECONDS.sleep(3l);
                    state = 0;
                    TimeUnit.SECONDS.sleep(1l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();

        //
        new Thread(() -> {

            while (state == 0) {
                state = 1;
                System.out.println("[线程：" + Thread.currentThread().getName() + "]加锁成功!");
                try {
                    TimeUnit.SECONDS.sleep(3l);
                    state = 0;
                    TimeUnit.SECONDS.sleep(1l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //
        new Thread(() -> {

            while (state == 0) {
                state = 1;
                System.out.println("[线程：" + Thread.currentThread().getName() + "]加锁成功!");
                try {
                    TimeUnit.SECONDS.sleep(3l);
                    state = 0;
                    TimeUnit.SECONDS.sleep(1l);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }).start();

    }

    private class TestRunnable implements Runnable {

        @Override
        public void run() {

        }
    }

}
